package com.tangkeyon.controller.current;

import com.tangkeyon.common.Const;
import com.tangkeyon.common.ResponseCode;
import com.tangkeyon.pojo.Shipping;
import com.tangkeyon.pojo.User;
import com.tangkeyon.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/6/13 20:21
 * @Version 1.0
 */
@Controller
@RequestMapping("/shipping/")
public class ShippingController {
    @Autowired
    private IShippingService iShippingService;

    /**
     * @Description: 添加收货地址，向页面返回主键
     * @param session
     * @param shipping
     * @return
     */
    @RequestMapping("add.do")
    @ResponseBody
    public ResponseCode add(HttpSession session, Shipping shipping){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
          return   ResponseCode.createByErrorMessage("请先登录");
        }
       return   iShippingService.add(user.getId(),shipping);
    }

    /**
     * @Description: 删除收货地址
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping("del.do")
    @ResponseBody
    public ResponseCode del(HttpSession session, Integer shippingId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return   ResponseCode.createByErrorMessage("请先登录");
        }
       return iShippingService.del(user.getId(),shippingId);
    }

    /**
     * @Description: 修改收货地址
     * @param session
     * @param shipping
     * @return
     */
    @RequestMapping("update.do")
    @ResponseBody
    public ResponseCode update(HttpSession session, Shipping shipping){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请先登录");
        }
        return iShippingService.update(user.getId(),shipping);
    }

    /**
     * @Description: 分页显示当前登录用户的所有收货地址
     * @param session
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ResponseCode list(HttpSession session,
                               @RequestParam(defaultValue = "3",required = false) Integer pageSize,
                               @RequestParam(defaultValue = "1",required = false) Integer pageNum){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ResponseCode.createByErrorMessage("请先登录");
        }
        return iShippingService.list(user.getId(),pageSize,pageNum);
    }

    /**
     * @Description: 获取当前登录用户指定的收货地址
     * @param session
     * @param shippingId
     * @return
     */
    @RequestMapping("select.do")
    @ResponseBody
    public ResponseCode select(HttpSession session, Integer shippingId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return    ResponseCode.createByErrorMessage("请先登录");
        }
        return iShippingService.select(user.getId(),shippingId);
    }









}
